package dev.hitools.noah.modules.sample.detail.extend.download

import android.content.Intent
import android.net.Uri
import android.os.Build
import android.os.Bundle
import android.util.Log
import android.view.View
import androidx.core.content.FileProvider
import com.arialyy.annotations.Download
import dev.hitools.common.extensions.toast
import dev.hitools.common.utils.download.DownloadManager
import dev.hitools.common.utils.download.DownloadStatusInfo
import dev.hitools.common.utils.download.DownloadTask
import dev.hitools.noah.R
import dev.hitools.noah.databinding.FSampleDownloadBinding
import dev.hitools.noah.modules.base.mvvm.view.AppBindFragment


/**
 * Created by yuhaiyang on 2020-03-05.
 */
class SampleDownloadFragment : AppBindFragment<FSampleDownloadBinding, SampleDownloadViewModel>() {
    private val url2 = "http://file.hiqidi.com/version-update/guanwang/adunpai.apk"

    //private val url = "https://adunpai.com/version-update1/1.1.1/guanwang/adunpai.apk"
    private val url = "https://imtt.dd.qq.com/16891/apk/A9CF9330B8F98FDA0702745A0EA2BDFC.apk"
    override fun getLayout(): Int = R.layout.f_sample_download

    override fun onViewCreated(view: View, savedInstanceState: Bundle?) {
        super.onViewCreated(view, savedInstanceState)
        binding.download1.setOnClickListener { download1() }
        binding.download2.setOnClickListener { Test().start() }
    }

    private fun download1() {
        try {
            DownloadManager.newTask()
                .url(url)
                .saveName("weixin1.apk")
                .savePath(requireContext().getExternalFilesDir("apk")!!.absolutePath)
                .setOnProgressListener { percent, speed -> update1(percent, speed) }
                .setOnStatusChangedListener { binding.status1.text = it.toString() }
                .start()
        } catch (e: Exception) {
            Log.e("yhy", "download1: e = $e")
        }
    }

    private fun update1(current: Float, speed: Long) {
        val text = "下载进度：$current   下载速度：$speed b/s"
        mainThread { binding.status1.text = text }
    }


    private fun installApp(info: DownloadStatusInfo) {
        val file = info.file
        Log.i(TAG, "installApp: file = " + file?.absoluteFile)
        if (file == null) return
        val context = context ?: return
        val intent = Intent(Intent.ACTION_VIEW)
        intent.flags = Intent.FLAG_ACTIVITY_NEW_TASK
        //判断是否是AndroidN以及更高的版本
        if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.N) {
            intent.addFlags(Intent.FLAG_GRANT_READ_URI_PERMISSION)
            val contentUri = FileProvider.getUriForFile(context, "${context.packageName}.fileprovider", file)
            intent.setDataAndType(contentUri, "application/vnd.android.package-archive")
        } else {
            intent.setDataAndType(Uri.fromFile(file), "application/vnd.android.package-archive")
        }
        Log.i(TAG, "installApp: start ===" + isMainThread())
        context.startActivity(intent)
    }


    companion object {
        private const val TAG = "SampleDownloadFragment"
    }
}